import Vue from 'vue'
import VueRouter from 'vue-router'
import cookie from '../static/js/cookie';


Vue.use(VueRouter);


//公共部分
import app from '../views/app/app';

//全局状态控制引入
import store from '../store/index'


import titleHead from '../views/title/title_head'
import login from '../views/login/login'
import home from '../views/Home'

import Index from '../views/index/index';

import QRcode from '../views/canteen_management/QRcode';
import ConsumptionStatistics from '../views/canteen_management/ConsumptionStatistics';

import Recharge from '../views/staff_management/recharge';

import Setting from '../views/setting/setting';
import Level from '../views/setting/level/level';
import User from '../views/setting/user/user';


var router = new VueRouter({
    linkActiveClass: 'selected',
    routes: [
        {
            path: '/app',
            name: 'app',
            component: app,
            children: [
                {
                    path: 'login',
                    name: 'login',
                    components: {
                        head: titleHead,
                        content: login,
                    },
                    meta: {
                        title: '登录',
                        need_log: false
                    }
                },
                {
                    path: 'home',
                    name: 'home',
                    components: {
                        head: titleHead,
                        content: home,
                    },
                    meta: {
                        title: '主页',
                        need_log: false
                    },
                    redirect: {name: 'index'}, //跳转到下级第一层
                    children: [
                        {
                            path: 'index',
                            name: 'index',
                            component: Index,
                            meta: {
                                title: '首页',
                                need_log: false
                            },
                        },
                        {
                            path: 'canteen_management/qr_code',
                            name: 'canteen_management/qr_code',
                            component: QRcode,
                            meta: {
                                title: '二维码生成',
                                need_log: false
                            },
                        },
                        {
                            path: 'consumption_statistics',
                            name: 'consumption_statistics',
                            component: ConsumptionStatistics,
                            meta: {
                                title: '消费统计',
                                need_log: false
                            },
                        },
                        {
                            path: 'staff_management/recharge',
                            name: 'staff_management/recharge',
                            component: Recharge,
                            meta: {
                                title: '餐补充值',
                                need_log: false
                            },
                        },
                        {
                            path: 'setting',
                            name: 'setting',
                            component: Setting,
                            meta: {
                                title: '系统设置',
                                need_log: false
                            },
                            redirect: {name: 'level'}, //跳转到下级第一层
                            children: [
                                {
                                    path: 'level',
                                    name: 'level',
                                    component: Level,
                                    meta: {
                                        title: '修改标题',
                                        need_log: false
                                    }
                                },
                                {
                                    path: 'adduser',
                                    name: 'adduser',
                                    component: User,
                                    meta: {
                                        title: '添加用户',
                                        need_log: false
                                    }
                                }
                            ]
                        },
                    ]
                },

            ]
        }
    ]
})


router.beforeEach((to, from, next) => {
    if (to != undefined) {
        if (to.meta.need_log) {
            console.log(to.meta.need_log);
            if (!store.state.userInfo.token) {
                next({
                    path: '/app/login',
                });
            } else {
                next();
            }
        } else {
            if (to.path === '/') {
                next({
                    path: '/app/home/index',
                });
            } else {
                next();
            }
        }
    } else {
        if (to.path === '/') {
            next({
                path: '/app/home/index',
            });
        } else {
            next();
        }
    }

})


router.afterEach((to, from, next) => {
    document.title = to.meta.title;
    /*document.title = to.matched[to.matched.length - 1].meta.title;*/
})

export default router;
